package br.com.saeng2D.formulario.caixa_forca_c_m;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import br.com.saeng2D.formulario.caixa.ContainerAnimado;
import br.com.saeng2D.formulario.caixa.ControleAnimado;
import br.com.saeng2D.formulario.caixa.Dialogo;
import br.com.saeng2D.objeto.forca.ForcaCentralizada;
import br.com.saeng2D.util.TextFieldNumero;

public class ControleContainerAnimadoForcaC extends ControleAnimado implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JLabel labelNM = new JLabel("N");
	private JTextField textFieldVariavelForca = new TextFieldNumero(Short.MAX_VALUE);
	private JRadioButton[] radios = new JRadioButton[4]; 
	
	public ControleContainerAnimadoForcaC(String tituloObjetoAnimado, ContainerAnimado containerAnimado, Dialogo dialogo, int x, int y, int largura, int altura) {
		super(tituloObjetoAnimado, containerAnimado, dialogo);
		montarLayout(x, y, largura, altura);
		registrarEvento();
		setBorder(BorderFactory.createRaisedBevelBorder());
		registrarEventoVariavelForca();
	}

	public void montarLayout(int x, int y, int largura, int altura) {
		setLayout(null);
		setBounds(x, y, largura, altura);

		labelTitulo.setBounds(70, 5, 150, 15);
		add(labelTitulo);

		containerAnimado.setBounds(75, 20, 100, 100);
		add(containerAnimado);
		textFieldVariavelForca.setBounds(48, altura - 70, largura - 100, 20);
		labelNM.setBounds(28 + largura - 75, altura - 70, 40, 20);
		add(textFieldVariavelForca);
		add(labelNM);
		ButtonGroup grupo = new ButtonGroup();
		int rotulo = 0;
		int desloc = 10;
		for(int i=0; i<radios.length; i++) {
			radios[i] = new JRadioButton("" + rotulo);
			rotulo += 90;
			radios[i].setBounds(25+desloc, altura - 35, 45, 30);
			radios[i].setMargin(new Insets(0, 0, 0, 0));
			grupo.add(radios[i]);
			add(radios[i]);
			desloc += 45;
		}
		radios[0].setSelected(true);
	}
	
	public void registrarEvento() {
		for(int i=0; i<radios.length; i++) {
			radios[i].addActionListener(this);
		}
	}

	public void actionPerformed(ActionEvent e) {
		JRadioButton radio = (JRadioButton) e.getSource();
		int grau = Integer.parseInt(radio.getText());
		//String s = Integer.toString(grau);// ;+ simboloGrau;
		ForcaCentralizada forcaCentralizada = (ForcaCentralizada)containerAnimado.getAbstratoAnimado();  
		//forcaCentralizada.setStringValorNewton(s);
		//textFieldStringValorNewton.setText(Integer.toString(grau));// + simboloGrau);
		forcaCentralizada.inicializacao();
		forcaCentralizada.rotacionar(grau);
		containerAnimado.repaint();
	}
	
	public String getVariavelForca() {
		return textFieldVariavelForca.getText();
	}

	public void registrarEventoVariavelForca() {
		textFieldVariavelForca.addFocusListener(new FocusListener() {
			public void focusLost(FocusEvent e) {
			}
			public void focusGained(FocusEvent e) {
				containerAnimado.selecionar();
			}
		});
		textFieldVariavelForca.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {
			}
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					dialogo.adicionarNaSuperficie();
				}
			}
			public void keyPressed(KeyEvent e) {
			}
		});
	}
}